home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / kevoSource / keybuf.c < prev    next >
Text File  |  1993-05-10  |  4KB  |  159 lines

  1. /* Kevo -- a prototype-based object-oriented language */
  2. /* (c) Antero Taivalsaari 1991-1993                   */
  3. /* Some parts (c) Antero Taivalsaari 1986-1988           */
  4. /* keybuf.c: Keyboard buffer operations               */
  5.  
  6. /*     
  7.     These operations are suited mainly to event-driven systems, 
  8.     and thus may not be needed in all implementations of Kevo.
  9. */
  10.  
  11. #include "global.h"
  12.  
  13. /*---------------------------------------------------------------------------*/
  14. /* Keyboard buffer operations */
  15.  
  16. /* Put one character to the task-specific text buffer */
  17. void putToKeyBuffer(thisTask, c)
  18. TASK**    thisTask;
  19. char     c;
  20. {
  21.   char* target = (char*)((*thisTask)->textBuffer->mfa);
  22.   int head = (*thisTask)->textHead;
  23.   int size = (*thisTask)->textBuffer->sfa;
  24.  
  25.     target[head++] = c;
  26.  
  27.     /* Grow the text buffer if needed (allocate 32 bytes more) */
  28.     /* For safety, keep an extra blank cell in the end of the buffer */
  29.     if (head+CELL >= size*CELL) {
  30.         resizeClosure((*thisTask)->textBuffer, size+8);
  31.     }
  32.     
  33.     ((*thisTask)->textHead)++;
  34. }
  35.  
  36.  
  37. /* Put a string to the task-specific text buffer */
  38. void textToKeyBuffer(thisTask, string)
  39. TASK**    thisTask;
  40. char*    string;
  41. {
  42.     while (*string) putToKeyBuffer(thisTask, *string++);
  43. }
  44.  
  45.  
  46. /* Put two consecutive carriage returns to the text buffer.
  47.     This operation is needed frequently, since our parser needs two
  48.     separators between lines.
  49. */
  50. void crsToKeyBuffer(thisTask)
  51. TASK** thisTask;
  52. {
  53.     putToKeyBuffer(thisTask, CR);
  54.     putToKeyBuffer(thisTask, CR);
  55. }
  56.  
  57.  
  58. /* Put a number (converted to text) to the task-specific text buffer */
  59. void numberToKeyBuffer(thisTask, number)
  60. TASK**    thisTask;
  61. int        number;
  62. {
  63.     sprintf(charbuffer, "%d", number);
  64.     textToKeyBuffer(thisTask, charbuffer);
  65. }    
  66.  
  67.  
  68. /* Get one character from the task-specific text buffer */
  69. char getFromKeyBuffer(thisTask)
  70. TASK** thisTask;
  71. {
  72.   char c;
  73.   char* target; 
  74.   int head = (*thisTask)->textHead;
  75.   int tail = (*thisTask)->textTail;
  76.     
  77.     if (tail == head) {
  78.         if (tail) eraseKeyBuffer(thisTask);
  79.         return (0);
  80.     }
  81.  
  82.     target = (char*)((*thisTask)->textBuffer->mfa);
  83.     c = target[tail++];
  84.  
  85.     ((*thisTask)->textTail)++;
  86.  
  87.     return(c);
  88. }
  89.  
  90.  
  91. /* Remove the latest character from the task-specific text buffer */
  92. void removeFromKeyBuffer(thisTask)
  93. TASK** thisTask;
  94. {
  95.   int head = (*thisTask)->textHead;
  96.   int tail = (*thisTask)->textTail;
  97.     
  98.     if (tail == head) {
  99.         if (tail) eraseKeyBuffer(thisTask);
  100.         return;
  101.     }
  102.  
  103.     ((*thisTask)->textHead)--;
  104. }
  105.  
  106.  
  107. /* Erase the task-specific text buffer */
  108. void eraseKeyBuffer(thisTask)
  109. TASK** thisTask;
  110. {
  111.   int* target;
  112.   int i;
  113.  
  114.     /* Initialize text pointers */
  115.     (*thisTask)->textHead = 0;
  116.     (*thisTask)->textTail = 0;
  117.  
  118.     /* Resize the text buffer back to its original size (128 bytes) */
  119.     resizeClosure((*thisTask)->textBuffer, 32);
  120.  
  121.     /* Blank the buffer with 128 zero bytes (32 CELLs) */
  122.     target = (int*)((*thisTask)->textBuffer->mfa);
  123.     for(i = 0; i < 32; i++) target[i] = 0;
  124. }
  125.  
  126.  
  127. /* 
  128.     Check if there is a command line available in the task-specific 
  129.     text buffer. Return address to the beginning of the command line.
  130. */
  131. char* lineAvailable(thisTask)
  132. TASK** thisTask;
  133. {
  134.   char* target;
  135.   int head      = (*thisTask)->textHead;
  136.   int tail      = (*thisTask)->textTail;
  137.   int i = tail;
  138.   
  139.     if (tail == head) {
  140.         if (tail) eraseKeyBuffer(thisTask);
  141.         return (NIL);
  142.     }
  143.  
  144.     target = (char*)((*thisTask)->textBuffer->mfa);
  145.  
  146.     /* Find the first CR after 'tail' */
  147.       while (target[i] != CR && i < head) i++; 
  148.  
  149.       if (target[i] == CR) {
  150.         /* Use two zeros as line separators */
  151.           target[i] = 0;
  152.           target[i+1] = 0;
  153.           (*thisTask)->textTail = i + 2;
  154.           return(&(target[tail]));
  155.       }
  156.       else return(NIL);
  157. }
  158.  
  159.